package com.dotools.utils;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;

public class DevicesUtils_Vivo {
    private static Boolean isVivoDevice = null;

    public static Boolean isVivoDevice() {
        if (isVivoDevice == null) {
            isVivoDevice = Build.BRAND.equalsIgnoreCase("vivo");
        }
        return isVivoDevice;
    }

    /**
     * 打开该应用在vivo的权限管理页面
     *
     * @param context
     * @return
     */
    public static boolean openVIVOSetting(Context context, int resultCode) {
        Intent intent = new Intent();
        intent.putExtra("packagename", context.getPackageName());
        try {
            PackageInfo pkg = context.getApplicationContext().getPackageManager().getPackageInfo(context.getApplicationContext().getPackageName(), 0);
            String appName = pkg.applicationInfo.loadLabel(context.getApplicationContext().getPackageManager()).toString();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("title", appName);
            intent.setComponent(ComponentName.unflattenFromString("com.vivo.permissionmanager/.activity.SoftPermissionDetailActivity"));
            intent.putExtra("packagename", context.getPackageName());
            if (context instanceof Activity) {
                Activity a = (Activity) context;
                a.startActivityForResult(intent, resultCode);
            } else {
                context.startActivity(intent);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 打开vivo的通知栏管理页面
     * @param context
     * @return
     */
    public static boolean openNotificationSetting(Context context) {
        Intent intent = new Intent();
        intent.setComponent(ComponentName.unflattenFromString("com.android.systemui/com.vivo.systemui.statusbar.notification.settings.StatusbarSettingActivity"));
        try {
            context.startActivity(intent);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取悬浮窗权限状态
     *
     * @param context
     * @return 该状态的定义和{@link android.app.AppOpsManager#MODE_ALLOWED}，MODE_IGNORED等值差不多，自行查阅源码
     */
    public static boolean getFloatPermissionStatus(Context context) {
        String packageName = context.getPackageName();
        Uri uri = Uri.parse("content://com.iqoo.secure.provider.secureprovider/allowfloatwindowapp");
        String selection = "pkgname = ?";
        String[] selectionArgs = new String[]{packageName};
        Cursor cursor = context.getContentResolver().query(uri, null, selection, selectionArgs, null);
        if (cursor != null) {
            cursor.getColumnNames();
            if (cursor.moveToFirst()) {
                int currentmode = 1;
                try {
                    currentmode = cursor.getInt(cursor.getColumnIndex("currentstate"));
                } catch (IllegalStateException e) {
                    currentmode = cursor.getInt(cursor.getColumnIndex("currentmode"));
                }
                cursor.close();
                return currentmode == 0;
            } else {
                cursor.close();
                return getNewFloatPermissionStatus(context);
            }
        } else {
            return getNewFloatPermissionStatus(context);
        }
    }

    /**
     * 判断vivo后台弹出界面
     * @param context
     * @return  1未开启 0开启
     */
    public static int checkBackstagePop(Context context) {
        String packageName = context.getPackageName();
        Uri uri2 = Uri.parse("content://com.vivo.permissionmanager.provider.permission/start_bg_activity");
        String selection = "pkgname = ?";
        String[] selectionArgs = new String[]{packageName};
        try {
            Cursor cursor = context
                    .getContentResolver()
                    .query(uri2, null, selection, selectionArgs, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    int currentmode = cursor.getInt(cursor.getColumnIndex("currentstate"));
                    cursor.close();
                    return currentmode;
                } else {
                    cursor.close();
                    return 1;
                }
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return 1;
    }

    /**
     * vivo在10版本下获取悬浮窗权限方法
     *
     * @param context
     * @return
     */
    private static boolean getNewFloatPermissionStatus(Context context) {
        String packageName = context.getPackageName();
        Uri uri2 = Uri.parse("content://com.vivo.permissionmanager.provider.permission/float_window_apps");
        String selection = "pkgname = ?";
        String[] selectionArgs = new String[]{packageName};
        Cursor cursor = context.getContentResolver().query(uri2, null, selection, selectionArgs, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                int currentmode = 1;
                try {
                    currentmode = cursor.getInt(cursor.getColumnIndex("currentstate"));
                } catch (IllegalStateException e) {
                    currentmode = cursor.getInt(cursor.getColumnIndex("currentmode"));
                }
                cursor.close();
                return currentmode == 0;
            } else {
                cursor.close();
                return false;
            }
        }
        return false;
    }

    /****
     * 跳转VIVO市场详细APP页面
     * @param act   必须是ACTIVITY
     * @param pkg  APP包名
     */
    public static void jump2VivoMarket(Activity act, String pkg) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("vivomarket://details?id=" + pkg));
        act.startActivity(intent);
    }
}
